package eu.boss.model;

import java.io.Serializable;
import java.text.DecimalFormat;

import org.codehaus.jackson.annotate.JsonIgnore;

/**
 * This class describes the Player model. It's the same as the server.
 * @author Arnaud
 *
 */
public class Player implements Serializable, Comparable<Object>{

	private static final long serialVersionUID = -1276288398299057124L;
	private String name;
	private ShadowColor color;
	private int wonShadow;
	private int lostShadow;
	private int wonHunter;
	private int lostHunter;
	private int wonNeutral;
	private int lostNeutral;

	public Player()
	{}
	
	public Player(String name, ShadowColor color, int wonShadow, int lostShadow,
			int wonHunter, int lostHunter, int wonNeutral, int lostNeutral) {
		super();
		this.name = name;
		this.color = color;
		this.wonShadow = wonShadow;
		this.lostShadow = lostShadow;
		this.wonHunter = wonHunter;
		this.lostHunter = lostHunter;
		this.wonNeutral = wonNeutral;
		this.lostNeutral = lostNeutral;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public ShadowColor getColor() {
		return color;
	}
	public void setColor(ShadowColor color) {
		this.color = color;
	}
	public int getWonShadow() {
		return wonShadow;
	}
	public void setWonShadow(int wonShadow) {
		this.wonShadow = wonShadow;
	}
	public int getLostShadow() {
		return lostShadow;
	}
	public void setLostShadow(int lostShadow) {
		this.lostShadow = lostShadow;
	}
	public int getWonHunter() {
		return wonHunter;
	}
	public void setWonHunter(int wonHunter) {
		this.wonHunter = wonHunter;
	}
	public int getLostHunter() {
		return lostHunter;
	}
	public void setLostHunter(int lostHunter) {
		this.lostHunter = lostHunter;
	}
	public int getWonNeutral() {
		return wonNeutral;
	}
	public void setWonNeutral(int wonNeutral) {
		this.wonNeutral = wonNeutral;
	}
	public int getLostNeutral() {
		return lostNeutral;
	}
	public void setLostNeutral(int lostNeutral) {
		this.lostNeutral = lostNeutral;
	}
	
	/**
	 * All the following methods are ignored during the serialization/deserialization
	 */
	
	@JsonIgnore
	public void reInit(){
		this.wonShadow = 0;
		this.lostShadow = 0;
		this.wonHunter = 0;
		this.lostHunter = 0;
		this.wonNeutral = 0;
		this.lostNeutral = 0;
	}
	
	@JsonIgnore
	public int getWins(){
		return wonShadow + wonHunter + wonNeutral;
	}
	
	@JsonIgnore
	public int getLosts(){
		return lostShadow + lostHunter + lostNeutral;
	}
	
	@JsonIgnore
	public String getRatio(){
		DecimalFormat DEC = new DecimalFormat("0.00");
		float res;
		float wins=(float)(getWins());
		float losts=(float)(getLosts());
		if (getWins()+getLosts()==0)
			res=0;
		else
		res= wins/(wins+losts);
		return DEC.format(res);	
	}
	
	@JsonIgnore
	public String getRatioS(){
		DecimalFormat DEC = new DecimalFormat("0.00");
		float res;
		if (getWonShadow()+getLostShadow()==0)
			res=0;
		else
		res= (float)(getWonShadow())/(float)((getWonShadow())+(float)(getLostShadow()));
		return DEC.format(res);	
	}
	
	@JsonIgnore
	public String getRatioH(){
		DecimalFormat DEC = new DecimalFormat("0.00");
		float res;
		if (getWonHunter()+getLostHunter()==0)
			res=0;
		else
		res= (float)(getWonHunter())/(float)((getWonHunter())+(float)(getLostHunter()));
		return DEC.format(res);	
	}
	
	@JsonIgnore
	public String getRatioN(){
		DecimalFormat DEC = new DecimalFormat("0.00");
		float res;
		if (getWonNeutral()+getLostNeutral()==0)
			res=0;
		else
		res= (float)(getWonNeutral())/(float)(getWonNeutral()+(float)getLostNeutral());
		return DEC.format(res);		
	}
	
	@JsonIgnore
	public void incrementWS()
	{
		wonShadow++;
	}
	
	@JsonIgnore
	public void incrementLS()
	{
		lostShadow++;
	}
	
	@JsonIgnore
	public void incrementWH()
	{
		wonHunter++;
	}
	
	@JsonIgnore
	public void incrementLH()
	{
		lostHunter++;
	}
	
	@JsonIgnore
	public void incrementWN()
	{
		wonNeutral++;
	}
	
	@JsonIgnore
	public void incrementLN()
	{
		lostNeutral++;
	}

	@JsonIgnore
	/**
	 * Used to sort list by ratio
	 */
	public int compareTo(Object o1) {
		if(!(o1 instanceof Player))
	        throw new ClassCastException();
	        return (((Player)o1).getRatio()).compareTo((this.getRatio()));
	}
	
}
